<?php

class DispatchnotesController extends Controller{
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
             
               'users'=>array('@'),
            ),
         
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    public function actionCreate($dispatch)
    {
        $user=Users::model()->findByPk(Yii::app()->user->id);
        if($user->employeeid!=NULL){
            $model = new Notes;
            $model->attributes = $_POST;
            $model->note = $_POST['note'];
            if(isset($_POST['private']) && $_POST['private']=='on'){
                $model->private=1;
            }
            $model->empno = $user->employeeid;
            $model->dispatch = $dispatch;
            if($model->save()==false){
                echo json_encode($model->getErrors());
            }
        }
        


    }
    public function loadModel($id)
    {
        $model = Notes::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
    public function actionDelete($id){
        $this->loadModel($id)->delete();
    }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $model->attributes = $_POST;
        $model->note = $_POST['note'];
                
        if($model->save()==false){
            echo json_encode($model->getErrors());
        }
    }
    public function actionList($dispatch, $type, $page = 1, $sort = NULL, $dir = NULL)
    {    
		
        if ($page > 0) $page--;
        $crit = new CDbCriteria();
        if ($type==0){
            $crit->condition = 'dispatch=:dispatch and empno IS NULL';
        }else if ($type==3){
            $crit->condition = "dispatch=:dispatch and empno IS NULL AND custno !=''";
        }else if ($type==4){
            $crit->condition = "dispatch=:dispatch and empno !='' AND custno is null";
        }else {
            $crit->condition = "dispatch=:dispatch and custno IS NULL and private IS NULL or private !=1";
        }
        
		$crit->params = array(
            'dispatch' => $dispatch,
        );

        $order = CJSON::decode($sort);
        if ($sort != NULL) {
            $crit->order = $order[0]['property'] . ' ' . $order[0]['direction'];
        }

        $data = new CActiveDataProvider('Notes', array(
            'criteria' => $crit,
            'pagination' => array(
                'pageSize' => 20,
                'currentPage' => $page,
            )
        ));

        if ($data != NULL && count($data->getData()) > 0) {
            $rs = array();
            $rs['totalCount'] = $data->getTotalItemCount();
            foreach ($data->getData() as $i => $item) {
                $rs['data'][$i] = $item->getAttributes();
                if (is_null($item->empno)) {
                    $rs['data'][$i]['addedby'] = 'customer';
                } else {
                    $rs['data'][$i]['addedby'] = Employees::getEmployeeName($item->empno);
                }
                $rs['data'][$i]['accessText'] = $item->getAccessText();
            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }
}
?>
